﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Project;
using System.IO;
using Descriptions.Model;
using Project.Model;

namespace IDEDialogs
{
    public partial class NewCodeFileDialog : Form
    {
        public string newFilePath;
        public Device device;
        DocumentType doctype;

        public NewCodeFileDialog(DocumentType doctype, List<Device> devices)
        {
            InitializeComponent();

            if (doctype.Comment != "")
                this.docTypeDescription.Text = doctype.Comment;
            else
                this.docTypeDescription.Text = "Добавление нового файла типа "+doctype.Name;

            foreach (var device in devices)
                if (device.Type.Name != "System")
                    devicesCmbBx.Items.Add(device);//(device.Type.Name + ":" + device.Name);
            devicesCmbBx.SelectedIndex = 0;
            this.doctype = doctype;
            
        }
        private void NameFileTextBox_Validating(object sender, CancelEventArgs e)
        {
            //if (DialogResult == AcceptButton.DialogResult)
            {
                string name = newFilePathTxtBx.Text.Trim();
                int indx = name.IndexOfAny(Path.GetInvalidFileNameChars());
                if (indx > 0)
                {
                    errorProvider1.SetError(newFilePathTxtBx, "Имя файла не может содержать символ: " + name[indx]);
                    e.Cancel = true;
                }
                if (name == "")
                {
                    errorProvider1.SetError(newFilePathTxtBx, "Имя файла должно быть задано");
                    e.Cancel = true;
                }
            }
        }

        private void NameFileTextBox_Validated(object sender, EventArgs e)
        {
            //if (DialogResult == AcceptButton.DialogResult)
            {

                string name = newFilePathTxtBx.Text.Trim();
                if (!name.EndsWith(doctype.Extension))
                {
                    name = name.Insert(name.Length, "."+doctype.Extension);
                }
                newFilePath = name;
                errorProvider1.SetError(newFilePathTxtBx, "");
            }
        }
        private void OKButton_Click(object sender, EventArgs e)
        {
            if (!ValidateChildren())
                DialogResult = DialogResult.None; // Валидация прошла неуспешно, отменяем закрытие формы
            else
            {
                device = (Device) devicesCmbBx.SelectedItem;
            }
        }
    }
}
